package logic

import (
	"context"
	"month_zg6/rpc/models/mysql"

	"month_zg6/rpc/goods"
	"month_zg6/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type TopLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTopLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TopLogic {
	return &TopLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TopLogic) Top(in *goods.TopRequest) (*goods.TopResponse, error) {
	var order []mysql.Goods
	err := mysql.DB.Model(&mysql.Order{}).Order("Goods_inventory desc").Find(&order).Error
	if err != nil {
		return nil, err
	}
	var str []*goods.WhereGoodsResponse
	for _, v := range order {
		str = append(str, &goods.WhereGoodsResponse{
			ID:             int64(v.ID),
			GoodsName:      v.GoodsName,
			GoodsInventory: int64(v.GoodsInventory),
			GoodsViews:     v.GoodsViews,
		})
	}
	return &goods.TopResponse{
		WhereGoodsResponse: str,
	}, nil
}
